#!/bin/sh

#  If the daemon is already executing in the background, find it's process and terminate it.
#  We need to do this BEFORE attempting to unload the driver because the driver won't unload
#  if the daemon is holding a reference to it.

r=`ps ax |grep SATARaid5Daemon | grep -v grep | grep -v $$ |grep -v $PPID | grep -v stop | grep -v start | awk '{print $1}'`
if [ "${r}" != "" ] ; then
    sudo /bin/kill -9 $r
fi

#  If an old version of the 3124 RAID-5 driver is loaded,
#  then remove it from the system.

a=`sudo kextstat | grep com.SiliconImage.driver.Si3124r5`
if [ "$a" != "" ] ; then
    sudo kextunload -b com.SiliconImage.driver.Si3124r5
fi

#  Also check to see if the non-RAID 3124 driver is loaded, and if it is, unload it as well.
#  We need to do this because either driver (RAID or non-RAID) can be loaded on any 3124
#  controller.
a=`sudo kextstat | grep com.SiliconImage.driver.Si3124u`
if [ "$a" != "" ] ; then
    sudo kextunload -b com.SiliconImage.driver.Si3124u
fi


# If the non-RAID driver is in the extensions directory we need to remove it from there as well.
if [ -d /System/Library/Extensions/SiliconImage3124u.kext ] ; then
   sudo rm -r /System/Library/Extensions/SiliconImage3124u.kext
   sudo touch /System/Library/Extensions
fi
